package com.z.system.config;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * Jackson配置类，用于自定义JSON序列化和反序列化行为
 */
@Configuration
public class JacksonConfig {

    /**
     * 配置ObjectMapper，处理日期格式等问题
     */
    @Bean
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        // 创建ObjectMapper
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        
        // 配置日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置时区为东八区
        objectMapper.setDateFormat(dateFormat);
        
        // 禁用默认的日期序列化功能（使用我们自定义的格式）
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        
        return objectMapper;
    }
}